/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.table.AbstractTableModel;
import org.balisunrise.common.GridColumn;
import org.balisunrise.common.Basic;
import org.balisunrise.common.EntityList;
import org.balisunrise.common.EntityListListener;
import org.balisunrise.common.ListChangedEvent;

/**
 *
 * @author Glauco
 */
public class BaliGridModel extends AbstractTableModel
        implements org.balisunrise.common.GridModel, EntityListListener{

    Class entityClass;
    String alias;
    EntityList entityList;
    GridColumn[] columns;
    Basic owner;
    String name;

    protected PropertyChangeSupport changeSupport =
            new PropertyChangeSupport(this);

    //
    // Métodos do AbstractTableModel
    //

    @Override
    public int getRowCount() {
        if (entityList != null)
            return entityList.getEntities().length;
        return 0;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columns[columnIndex].getColumnName();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columns[columnIndex].getColumnClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columns[columnIndex].isColumnEditable();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return columns[columnIndex].getValue(entityList.getEntityAt(rowIndex));
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (!aValue.getClass().equals( columns[columnIndex].getColumnClass()))
            throw new RuntimeException("Value Class is invalid.");

        columns[columnIndex].setValue(aValue, entityList.getEntityAt(rowIndex));
    }

    //
    // Métodos do GridModel
    //

    public Class getEntityClass() {
        return entityClass;
    }

    public String getAlias() {
        return alias;
    }

    public GridColumn[] getColumns() {
        return columns;
    }

    public EntityList getEntityList() {
        return entityList;
    }

    public void setEntityList(EntityList entityList) {
        if(this.entityList != entityList){
            if (this.entityList != null)
                this.entityList.removeEntityListener(0);
            if (entityList != null)
                entityList.addEntityListListener(this);
        }
        this.entityList = entityList;
    }
    
    public void setAlias(String alias) {
        this.alias = alias;
    }

    public void setColumns(GridColumn[] columns) {
        this.columns = columns;
    }

    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }

    //
    // Métodos do EntityListListener
    //

    public void entityListChanged(ListChangedEvent evt) {
        fireTableDataChanged();
    }

    public void entityAdded(ListChangedEvent evt) {
        fireTableRowsInserted(evt.getInitialIndex(), evt.getFinalIndex());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Basic getOwner() {
        return owner;
    }

    public void setOwner(Basic owner) {
        Basic oldOwner = this.owner;
        this.owner = owner;
        changeSupport.firePropertyChange("owner", oldOwner, owner);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
}
